package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.file.ImageReqVo;
import com.miplus.ccrm.core.controller.vo.file.ImageResVo;
import com.miplus.ccrm.core.controller.vo.file.UploadResVo;
import com.miplus.ccrm.core.service.FileService;
import com.miplus.ccrm.core.service.file.ImageService;
import com.miplus.ccrm.core.service.xsky.XSkyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-6.文件管理服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;
    @Resource
    private XSkyService xSkyService;
    @Resource
    private ImageService imageService;

    /**
     * 上载附件文件
     */

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public R<UploadResVo> upload(@RequestParam("file") MultipartFile file) {
        return R.Success(fileService.doUpload(file,false));
    }

    /**
     * 上载录音文件
     */

    @RequestMapping(value = "uploadRecord", method = RequestMethod.POST)
    public R<UploadResVo> uploadRecord(@RequestParam("file") MultipartFile file) {
        return R.Success(fileService.doUpload(file,true));
    }

    /**
     * 下载附件文件
     */
    @RequestMapping(value = "download", method = RequestMethod.GET)
    public R download(String skyid, HttpServletResponse response) {
        return R.Success(fileService.DownloadFile(skyid,response));
    }

    /**
     * 调听录音
     */
    @RequestMapping(value = "play", method = RequestMethod.GET)
    public R play(String skyid, HttpServletResponse response) {
        xSkyService.OnPlayRecord(skyid,response);
        //xSkyService.OnPlayFile(response);
        return R.Success();
    }

    /**
     * 取影像文件
     */
    @RequestMapping(value = "quyImageFiles", method = RequestMethod.GET)
    public R<List<ImageResVo>> quyImageFiles(@Validated ImageReqVo reqVo) {
        return R.Success(imageService.quyFiles(reqVo));
    }

    /**
     * 取影像图片Base64
     */
    @RequestMapping(value = "quyImageBase64", method = RequestMethod.GET)
    public R<String> quyImageFiles(@RequestParam @NotNull String fileId) {
        return R.Success(imageService.quyBase64(fileId));
    }

    /**
     * 取影像图片流
     */
    @RequestMapping(value = "quyImageStream", method = RequestMethod.GET)
    public R quyImageStream(String fileId,HttpServletResponse response) {
        imageService.quyImageStream(fileId,response);
        return R.Success();
    }
}
